# coding: utf-8
# @Time    : 2020/7/2 15:46
# @Author  : gongshuangxi
# @Email   : sxgong@topdatasec.com

import os
import sys
from config import conf
from flask import Flask
from libs.common import tdslogger

try:
    # python2
    reload(sys)
    sys.setdefaultencoding('utf-8')
except:
    # python3
    pass


#注册蓝图
def tds_register_blueprints(app):
    from apps.app0 import blueprint_app0
    app.register_blueprint(blueprint_app0(), url_prefix='/app0')



#创建核心对象
def create_app():
    app = Flask(__name__)
    tds_register_blueprints(app)
    return app


# 启动app
def start_app(app):
    host = conf.SERVER_HOST
    port = conf.SERVER_PORT
    if not isinstance(host, str) or len(host) == 0:
        tdslogger.error("invalid host")
        return False
    try:
        in_port = int(port)
        if in_port not in range(0, 65536):
            tdslogger.error("invalid port")
            return False
    except Exception as e:
        tdslogger.error("trans port to int exception: %s" % e)
        return False

    try:
        print(app.url_map)
        app.run(host=host, port=port, debug=True)
    except Exception as e:
        tdslogger.error("start apps exception: %s" % e)
        return False
